﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using TntOrm;
using SiteObjects;
using System.Data.SqlClient;

namespace TestOrm
{
    public partial class TestForm : Form
    {
        public TestForm( )
        {
            InitializeComponent( );
        }

        private void button1_Click( object sender, EventArgs e )
        {
            OpenConnection( );

            OrmManager Orm = OrmManager.GetInstance( );
            
            Orm.AddObject( typeof( SiteObjects.User ) );
            Orm.AddObject( typeof( SiteObjects.Role ) );
            Orm.AddObject( typeof( SiteObjects.Post ) );
            Orm.AddObject( typeof( SiteObjects.PostPhoto ) );
            Orm.AddObject( typeof( SiteObjects.Photo ) );

            Post PO = new Post( );
            PO.Deleted = false;
            Post[] ta = PO.FetchLookUp <Post>( );

            Post[] tt = PO.FetchLimit <Post>( 1, 1, false );
            //PO.PostID = 1;
            //Photo[] p = PO.Photos;

            //User UO = new User( );

            //UO.FetchByID( 1 );

            //Role RO = new Role( );
            //RO.CreateTable( );
            //RO.FetchByID( 1 );
            //User[] UOa = RO.RoleUsers;

            //PostPhoto pp = new PostPhoto( );
            //pp.CreateTable( );

            //Photo p = new Photo( );
            //p.CreateTable( );
        }

        private void OpenConnection( )
        {
            SqlConnectionStringBuilder ConnectionBuilder = new SqlConnectionStringBuilder( );

            ConnectionBuilder.DataSource = @"LOCALHOST\SQLEXPRESS";
            ConnectionBuilder.UserID = "User";
            ConnectionBuilder.Password = "666";
            ConnectionBuilder.InitialCatalog = "Come2Site";
            ConnectionBuilder.IntegratedSecurity = true;

            TntOrm.Utility.SqlHelper.Connection = new System.Data.SqlClient.SqlConnection( ConnectionBuilder.ToString( ) );
            TntOrm.Utility.SqlHelper.Connection.Open( );
        }

        private void TestForm_FormClosing( object sender, FormClosingEventArgs e )
        {
            if( TntOrm.Utility.SqlHelper.Connection.State == ConnectionState.Open )
            {
                TntOrm.Utility.SqlHelper.Connection.Close( );
            }
        }
    }
}